package common.codeGen;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class GenCreateService {
	@Autowired
	private GenService genService;
	
	public String create(GenBean genBean){
		StringBuffer sb = new StringBuffer();
		try {
			List<GenColumn> tableNameList = genService.getTableColumnName(genBean.getTableName());
			if(tableNameList==null||tableNameList.size()<=0){
				throw new IllegalArgumentException();
			}
			
			sb.append("package "+genBean.getPackagePath()+".service;").append("\n");
			sb.append("\n");
			sb.append("import org.springframework.beans.factory.annotation.Autowired;\n");
			sb.append("import org.springframework.stereotype.Controller;\n");
			sb.append("import org.springframework.stereotype.Service;\n");
			sb.append("import yeliang.database.bean.Page;\n");
			sb.append("import yeliang.database.bean.OrderBy;\n");
			
			sb.append("import business."+genBean.getTableName().toLowerCase()+".controller."+genBean.getTableName()+"Argument;\n");
			
			sb.append("import java.util.ArrayList;\n");
			sb.append("import java.util.List;\n");
			sb.append("import yeliang.database.interfaces.BaseDAO;\n");
			
			sb.append("import "+genBean.getPackagePath()+".dto."+genBean.getTableName()+";").append("\n");
			sb.append("@Service").append("\n");
			sb.append("public class "+genBean.getTableName()+"Service{\n");
			sb.append("\n");
			sb.append("@Autowired").append("\n");
			sb.append("private BaseDAO baseDao;").append("\n");
			sb.append(this.createInsertSQL(genBean.getTableName(), tableNameList)).append("\n");
			sb.append(this.createSelectSQL(genBean.getTableName(), tableNameList)).append("\n");
			sb.append("private static String DELETE_SQL=\" delete from "+genBean.getTableName()+" where id=?\";").append("\n");
			sb.append(this.createUpdateSQL(genBean.getTableName(), tableNameList)).append("\n");
			sb.append(this.createSelectMethod(genBean.getTableName())).append("\n");
			sb.append(this.createDeleteMethod(genBean.getTableName())).append("\n");
			sb.append(this.createUpdateMethod(genBean.getTableName())).append("\n");
			sb.append(this.createInsertMethod(genBean.getTableName())).append("\n");
			sb.append(this.createUpdateByObject(genBean.getTableName(),tableNameList)).append("\n");
			sb.append(this.createQueryObjectByArgument(genBean.getTableName(),tableNameList)).append("\n");
			
			sb.append("}").append("\n");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	public StringBuffer createSelectMethod(String tableName){
		StringBuffer sb = new StringBuffer();
		sb.append("public "+tableName+" getObjectById(String id)throws Exception{\n");
		sb.append("StringBuffer sb = new StringBuffer(SELECT_SQL);\n");
		sb.append("sb.append(\" and t.id=?\");\n");
		sb.append("return ("+tableName+")baseDao.getJavaBean(sb.toString(), "+tableName+".class, id);\n");
		sb.append("}\n");
		return sb;
	}
	
	public StringBuffer createDeleteMethod(String tableName){
		StringBuffer sb = new StringBuffer();
		sb.append("public int deleteById(String id)throws Exception{ \n");
		sb.append("int result = baseDao.commonUpdate(DELETE_SQL,id);\n");
		sb.append("return result;\n");
		sb.append("}\n");
		return sb;
	}
	
	public StringBuffer createInsertMethod(String tableName){
		StringBuffer sb = new StringBuffer();
		sb.append("public int insert(Object object)throws Exception{ \n");
		sb.append("int result = baseDao.update(INSERT_SQL, object);");
		sb.append("return result;");
		sb.append("}\n");
		return sb;
	}
	
	public StringBuffer createUpdateMethod(String tableName){
		StringBuffer sb = new StringBuffer();
		sb.append("public int update(Object obj)throws Exception{ \n");
		sb.append("int result = baseDao.update(UPDATE_SQL, obj);");
		sb.append("return result;");
		sb.append("}\n");
		return sb;
	}
	
	//INSERT SQL
	private StringBuffer createInsertSQL(String tableName,List<GenColumn> columnName){
		StringBuffer sb = new StringBuffer();
		sb.append("private static String INSERT_SQL = \"INSERT INTO "+tableName+"(");
		for(int i=0;i<columnName.size();i++){
			GenColumn str = columnName.get(i);
			if(i==columnName.size()-1){
				sb.append(str.getColumn_name());
			}else{
				sb.append(str.getColumn_name()).append(",");
			}
		}
		sb.append(") VALUES(");
		for(int i=0;i<columnName.size();i++){
			GenColumn str = columnName.get(i);
			if(i==columnName.size()-1){
				sb.append(":"+str.getColumn_name());
			}else{
				sb.append(":"+str.getColumn_name()).append(",");
			}
		}
		sb.append(")\";");
		return sb;
	}
	
//	private static String UPDATE_SQL =  "update sys_dictionary      "+
//	"set pid = :pid,         "+
//	"  code  = :code,      "+
//	"  name  = :name,      "+
//	"  remarks = :remarks, "+
//	"  createBy = :createBy,    "+
//	"  createdate = :createDate,"+
//	"  updateBy = :updateBy,    "+
//	"  updatedate  = :updateDate "+
//	" where id = :id";
		private StringBuffer createUpdateSQL(String tableName,List<GenColumn> columnName){
			StringBuffer sb = new StringBuffer();
			sb.append("private static String UPDATE_SQL = \"update  "+tableName+" set ");
			for(int i=0;i<columnName.size();i++){
				GenColumn str = columnName.get(i);
				if(i==columnName.size()-1){
					sb.append(str.getColumn_name() +" = :"+str.getColumn_name());
				}else{
					sb.append(str.getColumn_name() +" = :"+str.getColumn_name()).append(",");
				}
			}
			sb.append(" where id=:id\";");
			return sb;
		}
	

	private StringBuffer createSelectSQL(String tableName,List<GenColumn> columnName){
		StringBuffer sb = new StringBuffer();
		sb.append("private static String SELECT_SQL = \"SELECT ");
		for(int i=0;i<columnName.size();i++){
			GenColumn str = columnName.get(i);
			if(i==columnName.size()-1){
				sb.append("t."+str.getColumn_name());
			}else{
				sb.append("t."+str.getColumn_name()).append(",");
			}
		}
		sb.append(" FROM "+tableName +" t");
		sb.append(" WHERE 1=1 \";");
		return sb;
	}

	private StringBuffer createUpdateByObject(String tableName,List<GenColumn> columnName){
		StringBuffer sb = new StringBuffer();
		sb.append("private int updateObjectByDto("+tableName+" object) throws Exception{\n");
		sb.append(tableName+" "+tableName.toLowerCase()+"  = getObjectById(object.getId());\n");
		sb.append("if("+tableName.toLowerCase()+"!=null){\n");
		sb.append("new "+tableName+"Convert().objectChangeToUpdateObject(object, "+tableName.toLowerCase()+");\n");
		sb.append("return baseDao.update(UPDATE_SQL,"+tableName.toLowerCase()+");\n");
		sb.append("}else{\n");
		sb.append("throw new Exception(\"NOTEXISTSRECEIVERINFO\");\n");
		sb.append("}\n");
		sb.append("}\n");
		return sb;
	}

	
	private StringBuffer createQueryObjectByArgument(String tableName,List<GenColumn> columnName){
		
		StringBuffer sb = new StringBuffer();
		sb.append("public Page queryObjectByArgument("+tableName+"Argument argument,Page page,OrderBy[] orderBies) throws Exception{").append("\n");
		sb.append("	StringBuffer sb = new StringBuffer();").append("\n");
		sb.append("	Object[] objects = new "+tableName+"Convert().changeArgumentToQuery(argument,SELECT_SQL);").append("\n");
		sb.append("	List<String> argList = (List<String>)objects[0];").append("\n");
		sb.append("	sb.append(objects[1]);").append("\n");
		sb.append("	if(orderBies!=null&&orderBies.length>0){").append("\n");
		sb.append("		sb.append(\" order by \");").append("\n");
		sb.append("		for(int i=0;i<orderBies.length;i++){").append("\n");
		sb.append("			OrderBy orderBy = orderBies[i];").append("\n");
		sb.append("			if(i==orderBies.length-1){").append("\n");
		sb.append("				sb.append(orderBy.getColumn_name()).append(\" \").append(orderBy.getDirection());").append("\n");
		sb.append("			}else{").append("\n");
		sb.append("				sb.append(orderBy.getColumn_name()).append(\" \").append(orderBy.getDirection()).append(\",\");").append("\n");
		sb.append("			}").append("\n");
		sb.append("		}").append("\n");
		sb.append("	}").append("\n");
		sb.append("page = baseDao.getListByPage(sb.toString(), page, "+tableName+".class, argList.toArray());").append("\n");
		sb.append("return page;").append("\n");
		sb.append("}").append("\n");
		return sb;
	}
}
